package mcom.xmks.bluetooth.mesh.core;

import android.os.Bundle;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.List;

import mcom.xmks.bluetooth.mesh.constant.IntentConstant;
import mcom.xmks.bluetooth.mesh.storage.GroupItem;
import mcom.xmks.bluetooth.mesh.storage.GroupStorageUtil;
import mcom.xmks.bluetooth.mesh.storage.NodeItem;
import mcom.xmks.bluetooth.mesh.storage.StorageUtil;

public class NodeViewModel extends ViewModel {
    public MutableLiveData<List<NodeItem>> nodeList=new MutableLiveData<>();
    public MutableLiveData<List<GroupItem>>groupList=new MutableLiveData<>();
    public MutableLiveData<CustomEvent.Event>event=new MutableLiveData<>();
    public void refreshAllNodeState(){
        List<NodeItem> all = StorageUtil.getAllNode();
        nodeList.postValue(all);
    }
    public void refreshAllGroupState(){
        List<GroupItem> all = GroupStorageUtil.getAllGroup();
        groupList.postValue(all);
    }
    public void addNode(){
        event.postValue(new CustomEvent.Event(CustomEvent.EVENT_ADD_NODE,null));
    }
    public void addGroup(String name,int addr){
        GroupStorageUtil.putGroup(new GroupItem(name,addr,new ArrayList<>()));
        refreshAllGroupState();
    }
    public void addNodeToGroup(int nodeAddress,int groupAddress){
        GroupStorageUtil.addNodeToGroup(nodeAddress,groupAddress);
    }
    public void toNodeDetail(int netAddress,String name){
        if(netAddress!=-1){
            Bundle bundle =new Bundle();
            bundle.putInt(IntentConstant.BUNDLE_NODE_ADDRESS,netAddress);
            bundle.putString(IntentConstant.BUNDLE_NODE_NAME,name);
            event.postValue(new CustomEvent.Event(CustomEvent.EVENT_TO_NODE_DETAIL,bundle));
        }
    }
    public void forceDeleteNodeLocal(int netAddress,String name){
        if(netAddress!=-1){
            Bundle bundle = new Bundle();
            bundle.putInt(IntentConstant.BUNDLE_NODE_ADDRESS,netAddress);
            bundle.putString(IntentConstant.BUNDLE_NODE_NAME,name);
            event.postValue(new CustomEvent.Event(CustomEvent.EVENT_DELETRTE_NODE_LOCAL,bundle));
        }
    }
    public void toGroupDetail(int netAddress,String name){
        if(netAddress!=-1){
            Bundle bundle = new Bundle();
            bundle.putInt(IntentConstant.BUNDLE_GROUP_ADDRESS,netAddress);
            bundle.putString(IntentConstant.BUNDLE_GROUP_NAME,name);
            event.postValue(new CustomEvent.Event(CustomEvent.EVENT_TO_GROUP_DETAIL,bundle));
        }
    }
}
